using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace QLVMB
{
    public partial class frmTraCuu : Form
    {
        public frmTraCuu()
        {
            InitializeComponent();
        }

        public void XuatDuLieu(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, int i, List<DTO.ChuyenBay> ds1)
        {
            ds1.Add(ds[i]);
            int m = dgTraCuu.Rows.Count-2;
            dgTraCuu.Rows[m].Cells[0].Value = ds[i].MaChuyenBay;
            string s = ds[i].NgayBay.Day + "/" + ds[i].NgayBay.Month + "/" + ds[i].NgayBay.Year;
            dgTraCuu.Rows[m].Cells[1].Value = s;
            string d = ds[i].ThoiGian.Hour + ":" + ds[i].ThoiGian.Minute;
            dgTraCuu.Rows[m].Cells[2].Value = d;
            for (int j = 0; j < tensb.Count; j++)
            {
                if (ds[i].MaSanBayDi == tensb[j].MaSanBay)
                    dgTraCuu.Rows[m].Cells[3].Value = tensb[j].TenSanBay;
            }
            for (int j = 0; j < tensb.Count; j++)
            {
                if (ds[i].MaSanBayDen == tensb[j].MaSanBay)
                    dgTraCuu.Rows[m].Cells[4].Value = tensb[j].TenSanBay;
            }

            for (int k = 0; k < dt.Count; k++)
            {
                if (ds[i].MaChuyenBay == dt[k].MaChuyenBay)
                {

                    for (int j = 0; j < tensb.Count; j++)
                    {
                        if (dt[k].SanBayTrungGian == tensb[j].MaSanBay)
                            dgTraCuu.Rows[m].Cells[5].Value = tensb[j].TenSanBay;
                    }
                }

            }
            dgTraCuu.Rows[m].Cells[6].Value = ds[i].SoGheThuongGia;
            dgTraCuu.Rows[m].Cells[7].Value = ds[i].SoGhePhoThong;

        }

        
        private void frmTraCuu_Load(object sender, EventArgs e)
        {
            List<DTO.ChuyenBay> ds = new List<DTO.ChuyenBay>();
            List<DTO.ChiTietChuyenBay> dt = new List<DTO.ChiTietChuyenBay>();
            List<DTO.SanBay> tensb = new List<DTO.SanBay>();
            List<DTO.SanBay> tensb2 = new List<DTO.SanBay>();
            List<DTO.ChuyenBay> ds1 = new List<DTO.ChuyenBay>();
            //Them du lieu vao 2 Combobox
            BUS.SanBayBUSResult rs = BUS.SanBayBUS.ChonHet(tensb);
            BUS.SanBayBUSResult rs2 = BUS.SanBayBUS.ChonHet(tensb2);
            DTO.SanBay sb = new DTO.SanBay();
            sb.MaSanBay = 0;
            sb.TenSanBay = "";
            tensb.Insert(0, sb);
            tensb2.Insert(0, sb);
            cbSanBayDi.DataSource = tensb;
            cbSanBayDi.DisplayMember = "TenSanBay";
            cbSanBayDi.ValueMember = "MaSanBay";
            cbSanBayDen.DataSource = tensb2;
            cbSanBayDen.DisplayMember = "TenSanBay";
            cbSanBayDen.ValueMember = "MaSanBay";
            //Hoan tat them.

            //Them du lieu vao 3 comboBox Ngay,Thang,Nam
            for (int i = 1; i <= 31; i++)
            {

                cbNgay.Items.Add(i);
            }
            for (int i = 1; i <= 12; i++)
            {
                cbThang.Items.Add(i);
            }
            for (int i = 2000; i <= 2011; i++)
            {
                cbNam.Items.Add(i);
            }
            string tngay = "";
            string tthang = "";
            string tnam = "";
            cbNgay.Items.Insert(0, tngay);
            cbThang.Items.Insert(0, tthang);
            cbNam.Items.Insert(0, tnam);
            //cbNgay.SelectedItem = 0;
            //cbThang.SelectedItem = 0;
            //cbNam.SelectedItem = 0;
            //Hoan tat them.
            if (BUS.ChuyenBayBUS.ChonHetChuyenBay(ds) == BUS.ChuyenBayBUSResult.OK && BUS.SanBayBUS.ChonHet(tensb) == BUS.SanBayBUSResult.OK && BUS.ChiTietChuyenBayBUS.ChonHetCTChuyenBay(dt) == BUS.ChiTietChuyenBayBUSResult.OK)
            {
                for (int i = 0; i < ds.Count; i++)
                {

                    dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                    XuatDuLieu(ds, dt, tensb, i, ds1);


                }
            }
            
        }

        //Tra cuu theo MaChuyenBay
        public int TraCuuMaChuyenBay(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, List<DTO.ChuyenBay> ds1,string mcb)
        {
            int temp=0;
            for (int i = 0; i < ds.Count; i++)
            {
                if (mcb != "")
                {
                    if (mcb == ds[i].MaChuyenBay)
                    {
                        temp = 1;
                        dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                        XuatDuLieu(ds, dt, tensb, i, ds1);

                    }
                }
                    
            }
            return temp;
        }

        //Tra cuu theo SanBayDi
        public int TraCuuSanBayDi(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, List<DTO.ChuyenBay> ds1,int sbdi)
        {
            int temp = 0;
            for (int i = 0; i < ds.Count; i++)
            {
                if (sbdi != 0)
                {
                    if (sbdi == ds[i].MaSanBayDi)
                    {
                        temp = 1;
                        dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                        XuatDuLieu(ds, dt, tensb, i, ds1);
                    }
                }
            }
            return temp;
        }

        //Tra cuu theo SanBayDen
        public int TraCuuSanBayDen(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, List<DTO.ChuyenBay> ds1,int sbden)
        {
            int temp = 0;
            for (int i = 0; i < ds.Count; i++)
            {
                if (sbden != 0)
                {
                    if (sbden == ds[i].MaSanBayDen)
                    {
                        temp = 1;
                        dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                        XuatDuLieu(ds, dt, tensb, i, ds1);
                    }
                }                
            }
            return temp;
        }

        //Tra cuu theo ngaythangnam
        public int TraCuuTheoNgay(List<DTO.ChuyenBay> ds, List<DTO.ChiTietChuyenBay> dt, List<DTO.SanBay> tensb, List<DTO.ChuyenBay> ds1,int ngay,int thang,int nam)
        {
            int temp = 0;
            for (int i = 0; i < ds.Count; i++)
            {
                if (ngay != -1 && thang != -1 && nam != -1)
                {
                    if (ngay == ds[i].NgayBay.Day && thang == ds[i].NgayBay.Month && nam + 1999 == ds[i].NgayBay.Year)
                    {
                        temp = 1;
                        dgTraCuu.Rows.Insert(dgTraCuu.Rows.Count - 1);
                        XuatDuLieu(ds, dt, tensb, i, ds1);
                    }
                }
                
            }
            return temp;
        }

        private void btTraCuu_Click(object sender, EventArgs e)
        {
            dgTraCuu.Rows.Clear();
            List<DTO.ChuyenBay> ds = new List<DTO.ChuyenBay>();
            List<DTO.ChiTietChuyenBay> dt = new List<DTO.ChiTietChuyenBay>();
            List<DTO.SanBay> tensb = new List<DTO.SanBay>();
            List<DTO.ChuyenBay> ds1 = new List<DTO.ChuyenBay>();

            
            int ngay = int.Parse(cbNgay.SelectedIndex.ToString());
            int thang = int.Parse(cbThang.SelectedIndex.ToString());
            int nam = int.Parse(cbNam.SelectedIndex.ToString());
            int sbden = int.Parse(cbSanBayDen.SelectedValue.ToString());
            int sbdi = int.Parse(cbSanBayDi.SelectedValue.ToString());
            string mcb = txtMaChuyenBay.Text;
            if (BUS.ChuyenBayBUS.ChonHetChuyenBay(ds) == BUS.ChuyenBayBUSResult.OK && BUS.SanBayBUS.ChonHet(tensb) == BUS.SanBayBUSResult.OK && BUS.ChiTietChuyenBayBUS.ChonHetCTChuyenBay(dt) == BUS.ChiTietChuyenBayBUSResult.OK)
            {

                int temp;
                if ((ngay != -1 && thang == -1 && nam == -1) || (ngay == -1 && thang != -1 && nam == -1) || (ngay != -1 && thang != -1 && nam == -1) || (ngay == -1 && thang == -1 && nam != -1) || (ngay != -1 && thang == -1 && nam != -1) || (ngay == -1 && thang != -1 && nam != -1))
                {
                    MessageBox.Show("Ban phai chon day du ngay thang nam");
                }
                // MaChuyenBay
                if (sbdi == 0 && sbden == 0 && ngay == -1 && thang == -1 && nam == -1)
                {
                    temp = TraCuuMaChuyenBay(ds, dt, tensb, ds1,mcb);
                    ds1.Clear();
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Ngay
                //if (mcb == "" && sbdi == 0 && sbden == 0 && ngay != -1 && thang != -1 && nam != -1)
                //{

                //    temp = TraCuuTheoNgay(ds, dt, tensb, tenmb, ds1, ngay, thang, nam);
                //    ds1.Clear();
                //    if (temp == 0)
                //    {
                //        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                //    }
                //}
                //SanBayDi
                if (mcb == "" && sbden == 0 && ngay == -1 && thang == -1 && nam == -1)
                {
                    temp = TraCuuSanBayDi(ds, dt, tensb, ds1, sbdi);
                    ds1.Clear();
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //SanBayDen
                if (mcb == "" && sbdi == 0 && ngay == -1 && thang == -1 && nam == -1)
                {
                    temp = TraCuuSanBayDen(ds, dt, tensb, ds1, sbden);
                    ds1.Clear();
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //SanBayDi,SanBayDen
                if(mcb=="" && ngay == -1 && thang == -1 && nam == -1&&sbden!=0 &&sbdi!=0)
                {
                    temp = TraCuuSanBayDi(ds, dt, tensb, ds1,sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDen(ds1, dt, tensb, ds2,sbden);
                    if (temp == 0)
                    {
                            MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Ngay,SanBayDen
                if (mcb == "" && ngay != -1 && thang != -1 && nam != -1 && sbden != 0 && sbdi == 0)
                {
                    temp = TraCuuSanBayDen(ds, dt, tensb, ds1, sbden);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuTheoNgay(ds1, dt, tensb, ds2, ngay,thang,nam);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Ngay,SanBayDi
                if (mcb == "" && ngay != -1 && thang != -1 && nam != -1 && sbden == 0 && sbdi != 0)
                {
                    temp = TraCuuSanBayDi(ds, dt, tensb, ds1, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuTheoNgay(ds1, dt, tensb, ds2, ngay, thang, nam);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Machuyenbay,Sanbayden
                if (mcb != "" && ngay == -1 && thang == -1 && nam == -1 && sbden != 0 && sbdi == 0)
                {
                    temp = TraCuuSanBayDen(ds, dt, tensb, ds1, sbden);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, ds2, mcb);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Machuyenbay,SanBayDi
                if (mcb != "" && ngay == -1 && thang == -1 && nam == -1 && sbden == 0 && sbdi != 0)
                {
                    temp = TraCuuSanBayDi(ds, dt, tensb, ds1, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, ds2, mcb);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Machuyenbay,Ngay
                if (mcb != "" && ngay != -1 && thang != -1 && nam != -1 && sbden == 0 && sbdi == 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, ds1, ngay,thang,nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, ds2, mcb);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Ngay,SanBayDi,SanBayDen
                if (mcb == "" && ngay != -1 && thang != -1 && nam != -1 && sbden != 0 && sbdi != 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDi(ds1, dt, tensb, ds2, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds3 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDen(ds2, dt, tensb, ds3, sbden);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //MaChuyenBay,Ngay,SanBayDen
                if (mcb != "" && ngay != -1 && thang != -1 && nam != -1 && sbden != 0 && sbdi == 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, ds2, mcb);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds3 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDen(ds2, dt, tensb, ds3, sbden);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //Machuyenbay,ngaybay,sanbaydi
                if (mcb != "" && ngay != -1 && thang != -1 && nam != -1 && sbden == 0 && sbdi != 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, ds2, mcb);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds3 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDi(ds2, dt, tensb, ds3, sbdi);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
                //THEO 4 TIEU CHI
                if (mcb != "" && ngay != -1 && thang != -1 && nam != -1 && sbden != 0 && sbdi != 0)
                {
                    temp = TraCuuTheoNgay(ds, dt, tensb, ds1, ngay, thang, nam);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds2 = new List<DTO.ChuyenBay>();
                    temp = TraCuuMaChuyenBay(ds1, dt, tensb, ds2, mcb);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds3 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDi(ds2, dt, tensb, ds3, sbdi);
                    dgTraCuu.Rows.Clear();
                    List<DTO.ChuyenBay> ds4 = new List<DTO.ChuyenBay>();
                    temp = TraCuuSanBayDen(ds3, dt, tensb, ds4, sbden);
                    if (temp == 0)
                    {
                        MessageBox.Show("Khong tim thay thong tin trung voi thong tin ban da nhap. Vui long nhap lai ,^^!.");
                    }
                }
            }
        }

       

        
    }
}